package com.xca.is.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.xca.is.R;
import com.xca.is.databinding.ActivityBaiKeBinding;
import com.xca.is.entity.BaiKe;
import com.xca.is.http.HttpConnect;
import com.xca.is.model.BaiKeModel;
import com.xca.is.utils.EditTextUtils;
import com.xca.is.utils.WebViewScriptUtils;

public class BaiKeActivity extends AppCompatActivity implements HttpConnect {
    private ActivityBaiKeBinding binding ;

    private ViewModelProvider provider ;
    private BaiKeModel model ;
    private MutableLiveData<BaiKe> liveData ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityBaiKeBinding.inflate(getLayoutInflater());
        EdgeToEdge.enable(this);
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        int[] ints = new int[]{10,18} ;
        WebViewScriptUtils.htmlContent(ints,binding.baikeWeb);
        binding.baikeLoading.rLoading.setVisibility(View.GONE);
        binding.baikeError.queryError.setVisibility(View.GONE);
        binding.baikeMore.setVisibility(View.GONE);

        provider = new ViewModelProvider(this) ;
        model = provider.get(BaiKeModel.class) ;
        model.setModel(this);
        liveData = (MutableLiveData<BaiKe>) model.getLiveData() ;
        liveData.observe(this, new Observer<BaiKe>() {
            @Override
            public void onChanged(BaiKe baiKe) {
                if (baiKe.getCode()==200){
                    showData(baiKe);
                }
            }
        });
        onClickListener();

    }

    private void showData(BaiKe baiKe){
        Glide.with(this)
                .load(baiKe.getPic())
                .fitCenter()
                .error(R.drawable.image_error)
                .into(binding.baikeImage);
        binding.baikeText.setText(baiKe.getMsg());

        binding.baikeMore.setVisibility(View.VISIBLE);
        binding.baikeMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openWebsite(baiKe.getMore());
            }
        });
    }

    private void onClickListener(){
        binding.baikeSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = binding.baikeEdit.getText().toString().trim();
                if (!text.isEmpty()){
                    model.getData(text);
                }else {
                    binding.baikeText.setError("请输入内容");
                }
                EditTextUtils.clearFocus(binding.baikeEdit);
            }
        });
    }

    private void openWebsite(String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        } else {
            Toast.makeText(this, "无法打开网页，请检查设备是否安装了浏览器", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void Loading(boolean loading) {
        binding.baikeLoading.rLoading.setVisibility(loading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void Success(Object object) {

    }

    @Override
    public void Fail(String... msg) {

    }

    @Override
    public void Error(String msg) {
        if (!msg.isEmpty()){
            binding.baikeError.queryError.setVisibility(View.VISIBLE);
            binding.baikeLoading.rLoading.setVisibility(View.GONE);
        }
    }

    @Override
    public void Complete() {
        binding.baikeLoading.rLoading.setVisibility(View.GONE);
    }

    @Override
    public void Timeout() {

    }

    @Override
    public void TokenError() {

    }
}